<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>越南时事新闻汉越双语平行语料库 - 用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 顶部导航栏 */
        .header {
            background: #c41e3a;
            color: white;
            padding: 0 20px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .logo {
            display: flex;
            align-items: center;
            font-size: 18px;
            font-weight: bold;
        }

        .logo::before {
            content: "🏛️";
            margin-right: 8px;
            font-size: 20px;
        }

        .header-title {
            font-size: 16px;
            opacity: 0.9;
        }

        /* 主内容区域 */
        .main-container {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 40px 20px;
            position: relative;
        }

        /* 背景装饰 */
        .bg-decoration {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: 
                radial-gradient(circle at 20% 20%, rgba(196, 30, 58, 0.05) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(255, 215, 0, 0.05) 0%, transparent 50%);
            pointer-events: none;
        }

        /* 左侧插图区域 */
        .illustration-section {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            max-width: 600px;
            margin-right: 60px;
        }

        .illustration {
            width: 100%;
            max-width: 500px;
            height: 400px;
            background: #c41e3a;
            border-radius: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(196, 30, 58, 0.2);
        }

        .illustration::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: 
                radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 70% 70%, rgba(255, 215, 0, 0.1) 0%, transparent 50%);
        }

        .illustration-content {
            position: relative;
            z-index: 2;
            text-align: center;
            color: white;
        }

        .illustration-icon {
            font-size: 80px;
            margin-bottom: 20px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
            animation: pulse 2s ease-in-out infinite;
        }

        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.1); }
        }

        /* 添加装饰性元素 */
        .decoration-elements {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            pointer-events: none;
        }

        .decoration-square {
            position: absolute;
            border: 2px solid rgba(255, 255, 255, 0.3);
            animation: rotate-square 15s linear infinite;
        }

        .decoration-square:nth-child(1) {
            width: 50px;
            height: 50px;
            top: 15%;
            left: 15%;
            animation-delay: 0s;
        }

        .decoration-square:nth-child(2) {
            width: 30px;
            height: 30px;
            top: 70%;
            right: 20%;
            animation-delay: -3s;
        }

        .decoration-square:nth-child(3) {
            width: 70px;
            height: 70px;
            bottom: 15%;
            left: 25%;
            animation-delay: -6s;
        }

        @keyframes rotate-square {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* 添加闪烁星星效果 */
        .star {
            position: absolute;
            width: 6px;
            height: 6px;
            background: rgba(255, 255, 255, 0.8);
            clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
            animation: twinkle 4s ease-in-out infinite;
        }

        .star:nth-child(1) { top: 20%; left: 25%; animation-delay: 0s; }
        .star:nth-child(2) { top: 40%; right: 30%; animation-delay: -1s; }
        .star:nth-child(3) { bottom: 25%; left: 35%; animation-delay: -2s; }
        .star:nth-child(4) { bottom: 15%; right: 25%; animation-delay: -3s; }
        .star:nth-child(5) { top: 60%; left: 15%; animation-delay: -4s; }

        @keyframes twinkle {
            0%, 100% { 
                opacity: 0.3;
                transform: scale(1);
            }
            50% { 
                opacity: 1;
                transform: scale(1.3);
            }
        }

        .illustration-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }

        .illustration-subtitle {
            font-size: 16px;
            opacity: 0.9;
            text-shadow: 0 1px 2px rgba(0,0,0,0.3);
        }

        /* 注册表单区域 */
        .register-section {
            width: 450px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.1);
            padding: 40px;
            position: relative;
        }

        .register-section::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: #c41e3a;
            border-radius: 12px 12px 0 0;
        }

        .register-title {
            text-align: center;
            margin-bottom: 30px;
        }

        .register-title h1 {
            font-size: 24px;
            color: #c41e3a;
            margin-bottom: 8px;
            font-weight: bold;
        }

        .register-title p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: #fafafa;
        }

        .form-input:focus {
            outline: none;
            border-color: #c41e3a;
            background: white;
            box-shadow: 0 0 0 3px rgba(196, 30, 58, 0.1);
        }

        .form-input::placeholder {
            color: #999;
        }

        .verification-group {
            display: flex;
            gap: 10px;
        }

        .verification-input {
            flex: 1;
        }

        .verification-button {
            padding: 12px 20px;
            background: #ffd700;
            color: #333;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            white-space: nowrap;
        }

        .verification-button:hover {
            background: #e6c200;
            transform: translateY(-1px);
        }

        .verification-button:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }

        .register-button {
            width: 100%;
            padding: 14px;
            background: #c41e3a;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }

        .register-button:hover {
            background: #a0172e;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(196, 30, 58, 0.3);
        }

        .register-button:active {
            transform: translateY(0);
        }

        .login-link {
            text-align: center;
        }

        .login-link a {
            color: #c41e3a;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .login-link a:hover {
            color: #a0172e;
            text-decoration: underline;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .main-container {
                flex-direction: column;
                padding: 20px;
            }

            .illustration-section {
                margin-right: 0;
                margin-bottom: 40px;
                max-width: 100%;
            }

            .illustration {
                height: 300px;
            }

            .illustration-icon {
                font-size: 60px;
            }

            .illustration-title {
                font-size: 20px;
            }

            .register-section {
                width: 100%;
                max-width: 450px;
            }
        }

        /* 加载动画 */
        .loading {
            display: none;
            text-align: center;
            margin-top: 20px;
        }

        .loading-spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 2px solid #f3f3f3;
            border-top: 2px solid #c41e3a;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* 错误提示 */
        .error-message {
            background: #fee;
            color: #c41e3a;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            border-left: 4px solid #c41e3a;
            display: none;
        }

        /* 成功提示 */
        .success-message {
            background: #efe;
            color: #2d5a2d;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            border-left: 4px solid #2d5a2d;
            display: none;
        }

        /* 密码强度指示器 */
        .password-strength {
            margin-top: 8px;
            height: 4px;
            background: #e0e0e0;
            border-radius: 2px;
            overflow: hidden;
        }

        .password-strength-bar {
            height: 100%;
            transition: all 0.3s ease;
            border-radius: 2px;
        }

        .strength-weak { background: #ff4757; width: 33%; }
        .strength-medium { background: #ffa502; width: 66%; }
        .strength-strong { background: #2ed573; width: 100%; }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <header class="header">
        <div class="logo">
            <span>越南时事新闻汉越双语平行语料库</span>
        </div>
        <div class="header-title">用户注册</div>
    </header>

    <!-- 主内容区域 -->
    <main class="main-container">
        <div class="bg-decoration"></div>
        
        <!-- 左侧插图区域 -->
        <section class="illustration-section">
            <div class="illustration">
                <!-- 装饰性元素 -->
                <div class="decoration-elements">
                    <div class="decoration-square"></div>
                    <div class="decoration-square"></div>
                    <div class="decoration-square"></div>
                    <div class="star"></div>
                    <div class="star"></div>
                    <div class="star"></div>
                    <div class="star"></div>
                    <div class="star"></div>
                </div>
                
                <div class="illustration-content">
                    <div class="illustration-icon">👥</div>
                    <div class="illustration-title">加入我们</div>
                    <div class="illustration-subtitle">成为汉越双语语料库的专业用户</div>
                </div>
            </div>
        </section>

        <!-- 注册表单区域 -->
        <section class="register-section">
            <div class="register-title">
                <h1>帐号注册</h1>
                <p>请填写以下信息完成注册</p>
            </div>

            <!-- 错误提示 -->
            <div class="error-message" id="errorMessage"></div>
            
            <!-- 成功提示 -->
            <div class="success-message" id="successMessage"></div>

            <form id="registerForm">
                <div class="form-group">
                    <label class="form-label" for="username">用户名</label>
                    <input 
                        type="text" 
                        id="username" 
                        name="username" 
                        class="form-input" 
                        placeholder="请输入用户名（3-20个字符）"
                        required
                    >
                </div>

                <div class="form-group">
                    <label class="form-label" for="email">邮箱</label>
                    <input 
                        type="email" 
                        id="email" 
                        name="email" 
                        class="form-input" 
                        placeholder="请输入邮箱地址"
                        required
                    >
                </div>

                <div class="form-group">
                    <label class="form-label" for="verificationCode">邮箱验证码</label>
                    <div class="verification-group">
                        <input 
                            type="text" 
                            id="verificationCode" 
                            name="verificationCode" 
                            class="form-input verification-input" 
                            placeholder="请输入6位验证码"
                            maxlength="6"
                            required
                        >
                        <button type="button" class="verification-button" id="sendCodeBtn">
                            发送验证码
                        </button>
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-label" for="password">密码</label>
                    <input 
                        type="password" 
                        id="password" 
                        name="password" 
                        class="form-input" 
                        placeholder="请输入密码（6-20个字符）"
                        required
                    >
                    <div class="password-strength">
                        <div class="password-strength-bar" id="passwordStrengthBar"></div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-label" for="confirmPassword">确认密码</label>
                    <input 
                        type="password" 
                        id="confirmPassword" 
                        name="confirmPassword" 
                        class="form-input" 
                        placeholder="请再次输入密码"
                        required
                    >
                </div>

                <button type="submit" class="register-button">
                    注册
                </button>
            </form>

            <div class="login-link">
                <a href="login.html">已有账号？立即登录</a>
            </div>

            <!-- 加载动画 -->
            <div class="loading" id="loading">
                <div class="loading-spinner"></div>
                <span style="margin-left: 10px;">注册中...</span>
            </div>
        </section>
    </main>

    <script>
        // 发送验证码
        document.getElementById('sendCodeBtn').addEventListener('click', async function() {
            const email = document.getElementById('email').value.trim();
            const button = this;
            
            if (!email) {
                showError('请先输入邮箱地址');
                return;
            }
            
            // 邮箱格式验证
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(email)) {
                showError('请输入正确的邮箱格式');
                return;
            }
            
            // 禁用按钮并开始倒计时
            button.disabled = true;
            let countdown = 60;
            button.textContent = `${countdown}秒后重发`;
            
            const timer = setInterval(() => {
                countdown--;
                button.textContent = `${countdown}秒后重发`;
                if (countdown <= 0) {
                    clearInterval(timer);
                    button.disabled = false;
                    button.textContent = '发送验证码';
                }
            }, 1000);
            
            try {
                const response = await fetch('/auth/send-verification-code', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        email: email,
                        type: 'register'
                    })
                });
                
                const result = await response.json();
                
                if (response.ok && result.code === 200) {
                    showSuccess('验证码已发送，请查收邮件');
                } else {
                    showError(result.msg || '验证码发送失败');
                    // 重置按钮状态
                    clearInterval(timer);
                    button.disabled = false;
                    button.textContent = '发送验证码';
                }
                
            } catch (error) {
                console.error('发送验证码错误:', error);
                showError('网络错误，请稍后重试');
                // 重置按钮状态
                clearInterval(timer);
                button.disabled = false;
                button.textContent = '发送验证码';
            }
        });
        
        // 密码强度检测
        document.getElementById('password').addEventListener('input', function() {
            const password = this.value;
            const strengthBar = document.getElementById('passwordStrengthBar');
            
            let strength = 0;
            if (password.length >= 6) strength++;
            if (/[a-z]/.test(password)) strength++;
            if (/[A-Z]/.test(password)) strength++;
            if (/[0-9]/.test(password)) strength++;
            if (/[^a-zA-Z0-9]/.test(password)) strength++;
            
            strengthBar.className = 'password-strength-bar';
            if (strength <= 2) {
                strengthBar.classList.add('strength-weak');
            } else if (strength <= 4) {
                strengthBar.classList.add('strength-medium');
            } else {
                strengthBar.classList.add('strength-strong');
            }
        });
        
        // 注册表单处理
        document.getElementById('registerForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const email = document.getElementById('email').value.trim();
            const verificationCode = document.getElementById('verificationCode').value.trim();
            const password = document.getElementById('password').value.trim();
            const confirmPassword = document.getElementById('confirmPassword').value.trim();
            
            const errorMessage = document.getElementById('errorMessage');
            const successMessage = document.getElementById('successMessage');
            const loading = document.getElementById('loading');
            const registerButton = document.querySelector('.register-button');
            
            // 隐藏之前的消息
            errorMessage.style.display = 'none';
            successMessage.style.display = 'none';
            
            // 基本验证
            if (!username || !email || !verificationCode || !password || !confirmPassword) {
                showError('请填写所有必填字段');
                return;
            }
            
            if (username.length < 3 || username.length > 20) {
                showError('用户名长度必须在3-20个字符之间');
                return;
            }
            
            if (password.length < 6 || password.length > 20) {
                showError('密码长度必须在6-20个字符之间');
                return;
            }
            
            if (password !== confirmPassword) {
                showError('两次输入的密码不一致');
                return;
            }
            
            if (verificationCode.length !== 6) {
                showError('验证码必须是6位数字');
                return;
            }
            
            // 显示加载状态
            loading.style.display = 'block';
            registerButton.disabled = true;
            registerButton.textContent = '注册中...';
            
            try {
                const response = await fetch('/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        email: email,
                        password: password,
                        verificationCode: verificationCode
                    })
                });
                
                const result = await response.json();
                
                if (response.ok && result.code === 200) {
                    // 注册成功
                    showSuccess('注册成功！正在跳转到登录页面...');
                    
                    // 延迟跳转到登录页
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 2000);
                    
                } else {
                    // 注册失败
                    showError(result.msg || '注册失败，请检查输入信息');
                }
                
            } catch (error) {
                console.error('注册错误:', error);
                showError('网络错误，请稍后重试');
            } finally {
                // 隐藏加载状态
                loading.style.display = 'none';
                registerButton.disabled = false;
                registerButton.textContent = '注册';
            }
        });
        
        // 显示错误消息
        function showError(message) {
            const errorMessage = document.getElementById('errorMessage');
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }
        
        // 显示成功消息
        function showSuccess(message) {
            const successMessage = document.getElementById('successMessage');
            successMessage.textContent = message;
            successMessage.style.display = 'block';
        }
        
        // 检查是否已经登录
        window.addEventListener('load', function() {
            const token = localStorage.getItem('token');
            if (token) {
                // 如果已经登录，跳转到主页
                window.location.href = 'index.html';
            }
        });
    </script>
</body>
</html>
